package com.gitee.taotaojs.mybootsuper.mapper;


import com.gitee.taotaojs.mybootsuper.bean.BaseBean;
import com.gitee.taotaojs.mybootsuper.bean.BasePageBean;
import com.gitee.taotaojs.mybootsuper.bean.KeyValueBean;
import com.gitee.taotaojs.mybootsuper.bean.KeyValueList;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.Collection;
import java.util.List;

/**
 * sql包装类
 * **仅对于单表对象的处理**
 *
 *
 * @author TaoTaojs
 * @date 2020/4/21 3:17 PM
 * <p>Description</p>
 * 关联类:
 * 参考链接：
 * History:
 * <author>        <time>                      <version>          <desc>
 * TaoTaojs        2020/4/21 3:17 PM                V1.0          自定义sql关键字封装类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@Builder
@EqualsAndHashCode(callSuper = false)
public class MySqlBuild<T extends BaseBean> {

    /** 倒序排序字符串 */
    public static final String DESC = "DESC";

    /** 实例，单条件判断 */
    private T entity;

    /** select对象，如(id,doc_name) -> select id,doc_name */
    private String select;
    /** from对象，如(tb_user,tb_doc) -> form tb_user,tb_doc */
    private String from;
    /** join对象，如(left -> (tb_doc doc on doc.id = xx.doc_id)) -> left join tb_doc doc on doc.id = xx.doc_id */
    private Join join;

    /**
     * where字符串-逻辑运算符
     */
    private List<Where> dataWhere;
    /**
     * 列名称-正反序
     */
    private KeyValueList<String, String> order;
    /**
     * 分组
     */
    private String group;

    /**
     * 根据数据列表查询数据
     */
    private KeyValueList<String, Collection<?>> idsMapList;

    /**
     * 批量操作列表
     */
    private List<T> list;
    /**
     * 批量删除列表
     */
    private List<Object> deleteList;

    /** 基础分页功能 */
    private BasePageBean basePageBean;


    /**
     * 默认查询所有
     * @param ids 主键列表
     */
    public void setIdsMap(List<Integer> ids){
        if(idsMapList == null) {
            idsMapList = new KeyValueList<>();
        }
        idsMapList.add(new KeyValueBean<>("id", ids));
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString(callSuper = true)
    @Builder
    @EqualsAndHashCode(callSuper = false)
    private static class Join{
        /** 左连接 */
        private List<String> left;
        /** 右连接 */
        private List<String> right;
        /** 内连接 */
        private List<String> inner;
    }

    /**
     * where对象类
     */
    @Data
    @AllArgsConstructor
    @ToString(callSuper = true)
    @Builder
    @EqualsAndHashCode(callSuper = false)
    public static class Where {
        /** 条件字符串 */
        private String whereStr;
        /** 条件逻辑运算符 */
        @Builder.Default
        private Connection connection = Connection.AND;

        Where(){
            connection = Connection.AND;
        }

    }

    /**
     * 逻辑运算符
     */
    public enum Connection{
        /**
         * not
         */
        NOT(" not "),
        /**
         * and
         */
        AND(" and "),
        /**
         * or
         */
        OR(" or ");

        /**
         * 唯一标识符
         */
        private String code;

        Connection(String code){
            this.code = code;
        }

    }



}
